<!--修改已经发布的消息的组件-->
<template>
  <div class="changemessage">
    <!-- 修改按钮，点击显示编辑对话框 -->
    <div class="modify-text" @click="dialogFormVisible = true">修改</div>

    <!-- 商品信息编辑对话框 -->
    <el-dialog title="商品信息" :visible.sync="dialogFormVisible" class="modify-message">
      <el-form label-width="100px">
        <el-form-item label="商品图片" :label-width="formLabelWidth">
          <!-- 显示当前商品图片 -->
          <img
              :src="$store.state.imgShowRoad + '/file/' + cupdateGoodInfo.picture"
              alt=""
              class="order-img"/>
          <!-- 图片上传组件 -->
          <el-upload
              class="orders-img_el_upload"
              :action="upurl"
              :headers="upheaders"
              :limit="3"
              list-type="picture-card"
              :on-change="handleChange"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :on-error="handleError"
              :file-list="fileList"
              :class="{ disUoloadSty: noneBtnImg }"
              ref="upload"
          >
            <span class="orders-img_el_upload_btn" @click.stop="submitUpload"
            >上传图片</span
            >
          </el-upload>
        </el-form-item>
      </el-form>

      <!-- 商品信息表单 -->
      <el-form :model="form">
        <el-form-item label="商品标题" :label-width="formLabelWidth">
          <el-input
              v-model="cupdateGoodInfo.title"
              autocomplete="off"
              class="title"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品内容" :label-width="formLabelWidth">
          <textarea
              v-model="cupdateGoodInfo.content"
              autocomplete="off"
              class="content"
          ></textarea>
        </el-form-item>
        <el-form-item label="商品价格" :label-width="formLabelWidth">
          <el-input v-model="cupdateGoodInfo.price" autocomplete="off" class="price"></el-input>
          元
        </el-form-item>
      </el-form>

      <!-- 对话框底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click.stop="changeMessageClick"
        >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入更新订单API
import { updateOrderById } from "@/api/order";

export default {
  inject: ["reload"],
  data() {
    return {
      dialogImageUrl: "", // 预览图片URL
      dialogVisible: false, // 预览图片对话框显示控制
      showBtnImg: true, // 显示上传按钮
      noneBtnImg: false, // 不显示上传按钮
      limitCountImg: 3, // 最多上传3张图片
      upurl: this.$store.state.fileUploadRoad + "/file/upload/order", // 上传URL
      upheaders: {
        Authorization: window.localStorage.token, // 上传请求头，携带token
      },
      form: {}, // 表单数据
      fileList: [], // 上传的文件列表
      picture: "", // 图片名称
      dialogFormVisible: false, // 编辑对话框显示控制
      formLabelWidth: "120px", // 表单标签宽度
    };
  },
  props: {
    // 接收父组件传递的商品信息
    cupdateGoodInfo: {
      type: Object,
    },
  },
  methods: {
    // 上传失败处理
    handleError(err, file, fileList) {
      this.$message({
        message: "上传失败！",
        type: "success",
      });
      console.log(err);
    },

    // 上传成功处理
    handleSuccess(response, file, fileList) {
      if (file.response.flag === true) {
        this.fileList = fileList;
        this.cupdateGoodInfo.picture = file.response.data;
        if (fileList.length >= 3) {
          this.uploadDisabled = true; // 达到最大上传数量，禁用上传
        }
        alert(file.response.message);
      } else alert(file.response.data);
    },

    // 文件列表变化处理
    handleChange(file, fileList) {
      this.noneBtnImg = fileList.length >= this.limitCountImg;
    },

    // 删除文件处理
    handleRemove(file, fileList) {
      this.noneBtnImg = fileList.length >= this.limitCountImg;
      this.fileList.pop();
      this.uploadDisabled = false; // 恢复上传功能
    },

    // 预览图片处理
    handlePreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    // 提交上传
    submitUpload() {
      this.$refs.upload.submit();
    },

    // 修改商品信息提交处理
    changeMessageClick() {
      this.dialogFormVisible = false;
      // 调用API更新订单信息
      updateOrderById({
        order_id: this.$store.state.changedOrderId,
        title: this.cupdateGoodInfo.title,
        content: this.cupdateGoodInfo.content,
        price: this.cupdateGoodInfo.price,
        picture: this.cupdateGoodInfo.picture,
      })
          .then((res) => {
            if (res.flag === true) {
              this.reload(); // 刷新页面
              alert(res.message);
            } else {
              alert(res.data);
              this.reload();
            }
          })
          .catch((err) => {
            alert("修改失败");
          });
    },
  },
};
</script>

<style lang="less" scoped>
.changemessage {
  .modify-text {
    height: 25px;
    cursor: pointer;
    margin-right: 10px;
    color: #67C23A; // 修改按钮颜色
    &:hover{
      color: #035D1C; // 鼠标悬停颜色
    }
  }

  .el-dialog {
    width: 600px;

    // 商品图片样式
    .order-img {
      width: 148px;
      height: 148px;
      border-radius: 6px;
    }

    // 上传组件样式
    .orders-img_el_upload {
      height: 148px;
      .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;

        &:hover {
          border-color: #409eff; // 鼠标悬停时边框颜色
        }

        .el-upload__input {
          position: absolute;
          left: -1000px; // 隐藏原生上传按钮
        }
      }
    }

    // 表单输入框样式
    .title {
      width: 400px;
    }

    .content {
      width: 400px;
      height: 150px;
      resize: none; // 禁止调整大小
      outline: none;
      line-height: 1.5;
      border: 1px solid #dcdfe6;
    }

    .price {
      width: 100px;
    }
  }
}
</style>